
var phone = "15921032664"
var pass = "123456"
var oemCode = "0001"

//index.js
//获取应用实例
var util = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    senceList:{},
    deviceList:{}
  },
  //事件处理函数

  deviceTap:function(e){
 
  var index = parseInt(e.currentTarget.dataset.id)
  var obj = this.data.deviceList[index]
  var json = JSON.stringify(obj)

  var url = ""
  url =  '../device/device?deviceModle=' + json
  // if (obj.deviceTypeId === "0018") {
    
  // }else{
  //   url = '../device/testDevice/testDevice?deviceModle=' + json
  // }

   wx.navigateTo({
     
     url: url
     
   })


  },
  //下拉刷新
  onPullDownRefresh: function(){

      var that = this
     loadRequest(function(success,deviceList){
         
           if (success) {

              that.setData({
                  
                  deviceList:deviceList
            
              })

           }else{


           }
         

       },function(success,senceList){

          if (success) {
              wx.stopPullDownRefresh()

              that.setData({
                  
                  senceList:senceList
            
              })

           }else{

             wx.stopPullDownRefresh()
                wx.showToast({
                  title: "刷新失败",
                  icon: "",//仅支持success或者loading
                  duration: 2000,
                  success: function(res) {
                    
                  },
                  fail: function (res) {
                    
                  },
                  complete: function (res) {
                    
                  }
                });
           }


       })

    
   
  },

  onShow: function (){
     console.log('onShow -重新加载数据')
     var that = this

    



     loadRequest(function(success,deviceList){
         
           if (success) {

              that.setData({
                  
                  deviceList:deviceList
            
              })

           }
         

       },function(success,senceList){

          if (success) {
              that.setData({
                  
                  senceList:senceList
            
              })

           }


       })





  },
  onLoad: function () {
  
    var that = this
    util.login(phone,pass,oemCode,function(result,data){
     
      if (result){
        console.log("login success")
      
       loadRequest(function(success,deviceList){
         
           if (success) {

                  
              that.setData({
                deviceList:deviceList
              })

           }
         

       },function(success,senceList){

          if (success) {

              that.setData({
                  
                  senceList:senceList
            
              })

           }


       })



      
      }else{

        console.log(data)
      }
    });

    


  },
  
 
})



/*
 cb1 :回调是 deviceList
 cb2 :回调是 senceList

 */
function loadRequest(cb1,cb2){
   
   var loadSceneSuccess = false
   var loadDeviceSuccess = false
   var deviceList = {}
   var senceList = {}
    // util.test();
    util.test1();


         util.queryAllDeviceOnDesktop(function(result,data){
     //常用设备
        loadDeviceSuccess = result
          if (result) {
            
           deviceList = data
        }

         if (loadDeviceSuccess) {

           cb1(true,deviceList)
        }else{
          cb1(false,null)
        }

         });

        util.queryAllSceneOnDesktop(function(result,data){
     //常用场景
         loadSceneSuccess = result
        if (result) {
          
          senceList = data

        }

         if (loadDeviceSuccess) {

           cb2(true,senceList)
        }else{
          cb2(false,null)
        }


       });



       
  

}
